package cn.sciento.farm.devicemanager.api.jsonrpc.impl;

import cn.sciento.farm.devicemanager.api.jsonrpc.SceneJsonApi;
import cn.sciento.farm.devicemanager.common.constant.ResultConstant;
import cn.sciento.farm.devicemanager.dao.entity.DevDevice;
import cn.sciento.farm.devicemanager.dao.entity.DevScene;
import cn.sciento.farm.devicemanager.dao.model.SceneDeviceCell;
import cn.sciento.farm.devicemanager.dao.model.ResponseMessage;
import cn.sciento.farm.devicemanager.dao.model.SceneForm;
import cn.sciento.farm.devicemanager.service.DeviceControlService;
import cn.sciento.farm.devicemanager.service.SceneService;
import com.googlecode.jsonrpc4j.spring.AutoJsonRpcServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import static cn.sciento.farm.devicemanager.common.util.ReturnUtil.getReturnMap;

@Service
@AutoJsonRpcServiceImpl
public class SceneJsonApiImpl implements SceneJsonApi {

    @Autowired
    SceneService sceneService;

    @Autowired
    DeviceControlService deviceControlService;

    @Override
    public ResponseMessage create(SceneForm scene) {
        if(StringUtils.isEmpty(scene.getName())
        || StringUtils.isEmpty(scene.getType())
        || scene.getAreaId() <=0){
            return getReturnMap(ResultConstant.NULL_PARAMETER);
        }
        sceneService.updateDevice(scene.getId(),scene.getDeviceIdList());
        int count  = sceneService.insert(scene);
        return getReturnMap(ResultConstant.SUCCESS,count);
    }

    @Override
    public ResponseMessage update(SceneForm scene) {
        if(scene.getId()<=0){
            return getReturnMap(ResultConstant.NULL_PARAMETER);
        }
        SceneForm newScene = new SceneForm();
        newScene.setCoverImage(scene.getCoverImage());
        newScene.setName(scene.getName());
        newScene.setRemark(scene.getName());
        newScene.setLandId(scene.getLandId());
        newScene.setDeviceIdList(scene.getDeviceIdList());
        newScene.setId(scene.getId());
        newScene.setDefaultValue(scene.getDefaultValue());
        newScene.setStrategy(scene.getStrategy());
        sceneService.updateDevice(scene.getId(),scene.getDeviceIdList());
        int count  = sceneService.updateById(scene);
        return getReturnMap(ResultConstant.SUCCESS,count);
    }

    @Override
    public ResponseMessage delete(Long id) {
        int count  = sceneService.deleteById(id);
        return getReturnMap(ResultConstant.SUCCESS,count);
    }

    @Override
    public ResponseMessage get(Long id) {
        SceneForm devScene  = sceneService.getById(id);
        return getReturnMap(ResultConstant.SUCCESS,devScene);
    }

    @Override
    public ResponseMessage list(Integer areaId,Integer landId) {
        List<SceneForm> devSceneList = new ArrayList<>();
        if (landId == 0) {
            if(areaId <=0){

            }else {
                devSceneList = sceneService.getByAreaId(areaId);
            }
        }else {
            devSceneList = sceneService.getByLandId(landId);
        }
        return getReturnMap(ResultConstant.SUCCESS,devSceneList);
    }

    @Override
    public ResponseMessage listSceneDevice(Long id) {
        List<DevDevice> controllerList  = sceneService.getDeviceById(id);
        return getReturnMap(ResultConstant.SUCCESS,controllerList);
    }

    @Override
    public ResponseMessage listDeviceAll(Long id) {
        List<SceneDeviceCell> controllerList  = sceneService.getDeviceAllById(id);
        return getReturnMap(ResultConstant.SUCCESS,controllerList);
    }

    @Override
    public ResponseMessage control(Long sceneId, Long controllerId, Integer sw) {
        int count =0;
        if(sceneId <=0 && controllerId <=0){
            return getReturnMap(ResultConstant.NULL_PARAMETER);
        }
        if(sceneId <=0){
            count = deviceControlService.controlScene(sceneId,sw);
        }else {
            count = deviceControlService.controlSceneSwitch(controllerId,sw);
        }
        return getReturnMap(ResultConstant.SUCCESS,count);
    }

    @Override
    public ResponseMessage listDeviceId(Long id) {
        List<String> controllerList  = sceneService.getDeviceIdById(id);
        return getReturnMap(ResultConstant.SUCCESS,controllerList);
    }

    @Override
    public ResponseMessage updateSceneDevice(Long id, String list) {
        if(StringUtils.isEmpty(list)){
            return getReturnMap(ResultConstant.NULL_PARAMETER);
        }
        List<String> idList =Arrays.stream(list.split(",")).collect(Collectors.toList());
        int count   = sceneService.updateDevice(id,idList);
        return getReturnMap(ResultConstant.SUCCESS,count);
    }
}
